﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TraineeManagement.Models.BusinessObject
{
    public class TraineeBO : GenericBusiness<Trainee>
    {

        public List<Trainee> GetAllTrainees()
        {
            try
            {
                return base.GetAll();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<Trainee> GetNewTrainees()
        {
            try
            {
                DateTime dateToCompare = DateTime.Today.AddDays(-7);
                return base.GetList(t => (t.Sessions.OrderByDescending(s => s.StartDate).FirstOrDefault().StartDate) > (dateToCompare));
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<Trainee> GetExpiringTrainees()
        {
            try
            {
                DateTime dateToCompare = DateTime.Today.AddDays(7);
                return base.GetList(t => (
                    t.Sessions.OrderByDescending(s => s.StartDate).FirstOrDefault().EndDate < (dateToCompare))
                    && t.Sessions.OrderByDescending(s => s.StartDate).FirstOrDefault().EndDate > (DateTime.Today));
            }
            catch (Exception)
            {
                throw;
            }
        }

        public Trainee GetTraineeById(int traineeID)
        {
            try
            {
                return base.GetSingle(t => t.TraineeID == traineeID);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void AddTrainee(Trainee newTrainee)
        {
            try
            {
                base.Insert(newTrainee);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void UpdateTrainee(Trainee modifiedTrainee)
        {
            try
            {
                base.Update(modifiedTrainee);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void DeleteTrainee(Trainee trainee)
        {
            try
            {
                base.Delete(trainee);
            }
            catch (Exception)
            {

                throw;
            }
        }

    }
}